package com.bmt.pos.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.bmt.core.BaseContoller;
import com.bmt.core.ISaveable;
import com.bmt.core.PageListInfo;
import com.bmt.exception.ActionException;
import com.bmt.exception.ServiceException;
import com.bmt.pos.bean.User;
import com.bmt.pos.service.UserService;

@Controller
@RequestMapping(value = "usermgr")
public class UserContoller extends BaseContoller {
	@Autowired
	private UserService userService;

	@RequestMapping(value = "get/{id}.htm", method = RequestMethod.GET)
	public ModelAndView get(@PathVariable Integer id) throws Exception {
		User User = userService.getUserById(id);
		if (User == null) {
			throw new ActionException("User id [" + id + "] don't existed");
		}
		return new ModelAndView("user_detail", "user", User);
	}

	@RequestMapping(value = "userList.htm", method = RequestMethod.GET)
	public ModelAndView list() throws ServiceException {
		return new ModelAndView("redirect:page/1.htm");
	}

	@RequestMapping(value = "page/{pageNum}.htm", method = RequestMethod.GET)
	public ModelAndView getPage(@PathVariable Integer pageNum) throws Exception {
		ModelAndView mav = new ModelAndView("user_list");
		PageListInfo<ISaveable> plInfo = userService.getPageInfo(pageNum);
		mav.addObject("plInfo", plInfo);
		return mav;
	}
}
